<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" ref="searchForm" :model="filters">
            <el-form-item prop="date">
              <el-date-picker type="datetimerange" v-model="filters.date" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"> </el-date-picker>
            </el-form-item>
            <el-form-item prop="orderNo">
              <el-input v-model="filters.orderNo" placeholder="订单编号" clearable></el-input>
            </el-form-item>
            <el-form-item prop="billType">
              <el-select v-model="filters.billType" placeholder="账单类型" clearable>
                <el-option v-for="item in billTypeList" :value="item.id" :label="item.name" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="transactionId">
              <el-input v-model="filters.transactionId" placeholder="支付流水号" clearable></el-input>
            </el-form-item>
            <el-form-item prop="refundId">
              <el-input v-model="filters.refundId" placeholder="退款流水号" clearable></el-input>
            </el-form-item>
            <el-form-item prop="beeperName">
              <el-input v-model="filters.beeperName" placeholder="呼叫器名称" clearable> </el-input>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="dataList" :fit="true">
        <el-table-column label="序号" type="index" align="center"> </el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center"> </el-table-column>
        <el-table-column prop="orderNo" label="订单编号" align="center"></el-table-column>
        <el-table-column prop="beeperName" label="呼叫器" align="center"> </el-table-column>
        <el-table-column prop="userName" label="账单用户" align="center"></el-table-column>

        <el-table-column label="账单类型" align="center">
          <template slot-scope="scope">
            <div>
              {{ formatBillType(scope.row.billType) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="账单金额" align="center"></el-table-column>
        <el-table-column prop="content" label="账单内容" align="center"></el-table-column>
        <el-table-column prop="transactionId" label="支付流水号" align="center"></el-table-column>
        <el-table-column prop="payTime" label="支付时间" align="center"> </el-table-column>
        <el-table-column prop="refundId" label="退款流水号" align="center"> </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import BillApi from '@/api/finance/bill';
import * as stn from '@/constants/storeName';
export default {
  name: 'BilleManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      loadingIns: false,
      dataList: [],
      filters: {
        orderNo: '',
        businessId: '',
        date: [],
        billType: '',
        transactionId: '',
        retfundId: '',
        beeperName: '',
      },
      businessList: [],
      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
      },

      billTypeList: [
        { id: 1, name: '充值' },
        { id: 5, name: '订单收入' },
        { id: 10, name: '微信支付' },
        { id: 11, name: '微信支付差价' },
        { id: 15, name: '呼叫器余额支付' },
        { id: 16, name: '呼叫器余额支付差价' },
        { id: 21, name: '退款' },
        { id: 31, name: '提现' },
        { id: 32, name: '提现失败退款' },
      ],
    };
  },
  components: {},
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      this.initDate();
      this.getTableHeight();
      this.listTable();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    formatBillType(val) {
      switch (val) {
        case 1:
          return '充值';
        case 5:
          return '订单收入';
        case 10:
          return '微信支付';
        case 11:
          return '微信支付差价';
        case 15:
          return '呼叫器余额支付';
        case 16:
          return '呼叫器余额支付差价';
        case 21:
          return '退款';
        case 31:
          return '提现';
        case 32:
          return '提现失败退款';
      }
    },
    initDate() {
      let begin = new Date();
      begin.setHours(0);
      begin.setMinutes(0);
      begin.setSeconds(0);
      begin.setMonth(begin.getMonth() - 1);
      let end = new Date();
      this.filters.date = [begin, end];
    },
    listTable() {
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        orderNo: this.filters.orderNo,
        businessId: this.filters.businessId,
        billType: this.filters.billType,
        transactionId: this.filters.transactionId,
        refundId: this.filters.refundId,
        beeperName: this.filters.beeperName,
      };
      if (this.filters.date) {
        data.beginDate = this.$Utils.formatDate(this.filters.date[0], 'yyyy-MM-dd hh:mm:ss');
        data.endDate = this.$Utils.formatDate(this.filters.date[1], 'yyyy-MM-dd hh:mm:ss');
      }
      BillApi.listBill(data).then((res) => {
        this.page.pageTotal = res.data.total;
        this.dataList = res.data.list;
      });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.initDate();
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
